package cn.ibobo.lease.app.service.impl;

import cn.ibobo.lease.app.dao.SmsCodeMapper;
import cn.ibobo.lease.app.dao.UserMapper;
import cn.ibobo.lease.app.service.UserService;
import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.common.dto.AdminDTO;
import cn.ibobo.lease.common.dto.SmsCodeDTO;
import cn.ibobo.lease.common.dto.UserDTO;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.Md5Util;
import cn.ibobo.lease.common.util.UuidUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;


/**
 * 会员信息表业务处理接口
 * @Author lililiang
 * @Date 2018-12-13 18:36:40
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserDTO, String> implements UserService {

    private static final Log LOGGER = LogFactory.getLog(UserServiceImpl.class);

    @Autowired
    private UserMapper mapper;
    @Autowired
    private SmsCodeMapper smsCodeMapper;

    @Override
    protected BaseMapper<UserDTO, String> getMapper() {
        return mapper;
    }

    /**
     * 会员信息表列表
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public List<UserDTO> list4Page(UserDTO entity, Integer pageNum, Integer... pageSize) {
        return super.list4Page(entity, pageNum, pageSize);
    }

    /**
     * 会员信息表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public List<UserDTO> listByCondition(UserDTO entity) {
        return super.listByCondition(entity);
    }

    /**
     * 查询会员信息表详情信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public UserDTO queryOneByPk(String id) {
        return super.queryOneByPk(id);
    }

    /**
     * 保存会员信息表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer save(UserDTO entity) {
        //1.对密码进行加密处理
        String passwd = entity.getPasswd();
        if(passwd != null && passwd.length() > 0){
            entity.setPasswd(Md5Util.md5Suffix(passwd));
        }
        //2.用户账号重复校验
        if(checkRepeat(entity)){
            return ResultEnum.ACCOUNT_REPEAT.getCode();
        }
        String uid = entity.getUid();
        if(uid == null || uid.length() == 0){
            entity.setUid(UuidUtil.getUuid());
            //3. 增加短信验证码的校验
            SmsCodeDTO smsCodeDTO = new SmsCodeDTO();
            smsCodeDTO.setMobile(entity.getMobile());
            smsCodeDTO.setCode(entity.getCode());
            if(smsCodeMapper.check(smsCodeDTO) <= 0){
                return ResultEnum.NO_SMS_CODE.getCode();
            }
            //4. 注册用户
            if(mapper.add(entity) == 0){
                return ResultEnum.FAIL.getCode();
            }
        }else{
            if(mapper.edit(entity) == 0){
                return ResultEnum.FAIL.getCode();
            }
        }
        return ResultEnum.SUCCESS.getCode();
    }


    /**
     * @Description 重置密码
     * @Author lililiang
     * @Date 2019/3/4 14:28
     * @Param [entity]
     * @Return java.lang.Integer
     */
    @Override
    @Transactional
    public Integer changePassWord(UserDTO entity){
        //1. 校验短信验证码
        SmsCodeDTO smsCodeDTO = new SmsCodeDTO();
        smsCodeDTO.setMobile(entity.getMobile());
        smsCodeDTO.setCode(entity.getCode());
        if(smsCodeMapper.check(smsCodeDTO) <= 0){
            return ResultEnum.NO_SMS_CODE.getCode();
        }
        //2. 密码加密，并重置密码
        entity.setPasswd(Md5Util.md5Suffix(entity.getPasswd()));
        return mapper.changePassWord(entity);
    }


    /**
     * @Description 校验账号是否存在
     * @Author lililiang
     * @Date 2018/12/27 8:09
     * @Param [entity]
     * @Return boolean
     */
    private boolean checkRepeat(UserDTO entity) {
        UserDTO tempDTO = new UserDTO();
        tempDTO.setStatus(null);
        tempDTO.setName(entity.getName());
        String uid = entity.getUid();
        tempDTO.setUid(uid);
        List<UserDTO> userDTOS = mapper.listByCondition(tempDTO);
        if(userDTOS != null && (userDTOS.size() > 1
                || (userDTOS.size() ==1 && !userDTOS.get(0).getUid().equals(uid)))){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 删除会员信息表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer delete(String id) {
        return super.delete(id);
    }
}
